<?php 

   //FOR DEBUGGING--comment out when done!
   ini_set('display_errors', 1);
   ini_set('error_reporting', E_ALL ^ E_NOTICE);
   error_reporting(1);

   	session_start();
   
include('../includes/db_connect.php'); 
//include('../includes/phpheader.php');


  $id = $_POST["id"];
  $userId = $_SESSION['user_id'];

  $status = $_POST["status"];
  if(!$status)		$status=0; 		else 	$status=1;

   //echo "$id <br /> $status";
   //exit();

  $csr = $_POST["csr"];
  if(!$csr)		$csr=0; 		else 	$csr=1;

  $ilp = $_POST["ilp"];
  if(!$ilp)		$ilp=0; 		else 	$ilp=1;

  $ilpWritten = $_POST["ilpWritten"];
  if(!$ilpWritten)		$ilpWritten=0; 		else 	$ilpWritten=1;

  $csp = $_POST["csp"];
  if(!$csp)		$csp=0; 		else 	$csp=1;

  $ocp = $_POST["ocp"];
  if(!$ocp)		$ocp=0; 		else 	$ocp=1;

if($_POST["completionDateMonth"]!="") {
	$completionDateMonth = $_POST["completionDateMonth"];
	$completionDateDay = $_POST["completionDateDay"];
	$completionDateYear = $_POST["completionDateYear"];
	$completionDate = $completionDateYear."-".$completionDateMonth."-".$completionDateDay;
}

$primaryILPGoal = $_POST["primaryILPGoal"];
$frequencyContact = $_POST["frequencyContact"];

if($_POST["firstContactDateMonth"]!="") {
	$firstContactDateMonth = $_POST["firstContactDateMonth"];
	$firstContactDateDay = $_POST["firstContactDateDay"];
	$firstContactDateYear = $_POST["firstContactDateYear"];
	$firstContactDate = $firstContactDateYear."-".$firstContactDateMonth."-".$firstContactDateDay;
}

$consumerResponsibilities = $_POST["consumerResponsibilities"];
$coordinatorResponsibilities = $_POST["coordinatorResponsibilities"];

if($_POST["targetCompletionDateMonth"]!="") {
	$targetCompletionDateMonth = $_POST["targetCompletionDateMonth"];
	$targetCompletionDateDay = $_POST["targetCompletionDateDay"];
	$targetCompletionDateYear = $_POST["targetCompletionDateYear"];
	$targetCompletionDate = $targetCompletionDateYear."-".$targetCompletionDateMonth."-".$targetCompletionDateDay;
} 

if($_POST["actualCompletionDateMonth"]!="") {
	$actualCompletionDateMonth = $_POST["actualCompletionDateMonth"];
	$actualCompletionDateDay = $_POST["actualCompletionDateDay"];
	$actualCompletionDateYear = $_POST["actualCompletionDateYear"];
	$actualCompletionDate = $actualCompletionDateYear."-".$actualCompletionDateMonth."-".$actualCompletionDateDay;
} 

$ilpCopySignoff = $_POST["ilpCopySignoff"];
if(!$ilpCopySignoff)		$ilpCopySignoff=0; 		else 	$ilpCopySignoff=1;

$ilpConsumerSig = $_POST["ilpConsumerSig"];
$ilpGuardianSig = $_POST["ilpGuardianSig"];
$ilpSocialServiceSig = $_POST["ilpSocialServiceSig"];
$ilpCoordinatorSig = $_POST["ilpCoordinatorSig"];
$csrConsumerSig = $_POST["csrConsumerSig"];





/*

  HANDLE DATA REQUEST AND UPDATE APPROPRIATE RECORD

*/

   //GET THE CORRECT ILP FROM DB
   $query = "select id 
             from ilpcsr 
             where consumer_id = ".$id;


   //DETERMINE IF RECORD IS NEW OR EXISTING
   if(mysql_fetch_array(mysql_query($query))) {

       //DATA WAS FOUND, SO JUST UPDATE RECORD  
	$query = "UPDATE `ilpcsr` 
                  set `consumer_id`                   = ".$id.",
                      `status`                        = ".$status." ,  
                      `csr`                           = ".$csr." , 
                      `ilp`                           = ".$ilp." , 
                      `ilp_written`                   = ".$ilpWritten." , 
                      `csp`                           = ".$csp." , 
                      `ocp`                           = ".$ocp." , 
                      `completion_date`               = '".$completionDate."', 
                      `primary_ilp_goal`              = '".$primaryILPGoal."' , 
                      `frequency_contact`             = '".$frequencyContact."' , 
                      `first_contact_date`            = '".$firstContactDate."', 
                      `consumer_responsibilities`     = '".$consumerResponsibilities."', 
                      `coordinator_responsibilities`  = '".$coordinatorResponsibilities."', 
                      `target_completion_date`        = '".$targetCompletionDate."', 
                      `actual_completion_date`        = '".$actualCompletionDate."' , 
                      `ilp_copy_signoff`              = ".$ilpCopySignoff.", 
                      `ilp_consumer_sig`              = '".$ilpConsumerSig."', 
                      `ilp_guardian_sig`              = '".$ilpGuardianSig."' , 
                      `ilp_social_service_sig`        = '".$ilpSocialServiceSig."', 
                      `ilp_coordinator_sig`           = '".$ilpCoordinatorSig."', 
                      `csr_consumer_sig`              = '".$csrConsumerSig."' 
                   where consumer_id = ".$id;

   } else {

        //NO DATA RETRIEVED, CREATE NEW INSTANCE FOR RECORD
	$query = "INSERT INTO `ilpcsr` ( `id` , 
                                         `consumer_id` ,
                                         `status` , 
                                         `csr` , 
                                         `ilp` , 
                                         `ilp_written` , 
                                         `csp` , 
                                         `ocp` , 
		                         `completion_date` , 
                                         `primary_ilp_goal` , 
                                         `frequency_contact` , 
                                         `first_contact_date` , 
                                         `consumer_responsibilities` ,
		                         `coordinator_responsibilities` , 
                                         `target_completion_date` , 
                                         `actual_completion_date` , 
                                         `ilp_copy_signoff` ,
		                         `ilp_consumer_sig` , 
                                         `ilp_guardian_sig` , 
                                         `ilp_social_service_sig` , 
                                         `ilp_coordinator_sig` , 
                                         `csr_consumer_sig` , 
		                         `entered_by`)
		VALUES (NULL, ".$id.", ".$status.", ".$csr.", ".$ilp.", ".$ilpWritten.", ".$csp.", ".$ocp.", 
                        '".$completionDate."', '".$primaryILPGoal."', '".$frequencyContact."', 
                        '".$firstContactDate."', '".$consumerResponsibilities."', 
                        '".$coordinatorResponsibilities."', '".targetCompletionDate."', 
                        '".$actualCompletionDate."', ".$ilpCopySignoff.", '".$ilpConsumerSig."', 
                        '".$ilpGuardianSig."', '".$ilpSocialServiceSig."', '".$ilpCoordinatorSig."', 
                        '".$csrConsumerSig."', ".$userId.")";
   }


if(mysql_query($query)) {
	$ilpQuery = "select id from ilpcsr where consumer_id = ".$id;
	$row = mysql_fetch_array(mysql_query($ilpQuery));
	$ilpId = $row["id"];
	//echo $ilpId."<br/>";

	for($i=1; $i<5; $i++) {
		if($_POST["coordinatorInvolved".$i]!="") {
			$coordinatorInvolved = $_POST["coordinatorInvolved".$i];
			$ilpObjective = $_POST["ilpObjective".$i];
			if(!$ilpObjective)		$ilpObjective=0; 		else 	$ilpObjective=1;
			
			if($_POST["goalSetDate".$i."Month"]) {
				$goalSetDateMonth = $_POST["goalSetDate".$i."Month"];
				$goalSetDateDay = $_POST["goalSetDate".$i."Day"];
				$goalSetDateYear = $_POST["goalSetDate".$i."Year"];
				$goalSetDate = $goalSetDateYear."-".$goalSetDateMonth."-".$goalSetDateDay;
			} else {
				$goalSetDate = null;
			}
			
			if($_POST["goalTargetDate".$i."Month"]) {
				$goalTargetDateMonth = $_POST["goalTargetDate".$i."Month"];
				$goalTargetDateDay = $_POST["goalTargetDate".$i."Day"];
				$goalTargetDateYear = $_POST["goalTargetDate".$i."Year"];
				$goalTargetDate = $goalTargetDateYear."-".$goalTargetDateMonth."-".$goalTargetDateDay;
			} else {
				$goalTargetDate = null;
			}
			
			if($_POST["goalMetDate".$i."Month"]) {
				$goalMetDateMonth = $_POST["goalMetDate".$i."Month"];
				$goalMetDateDay = $_POST["goalMetDate".$i."Day"];
				$goalMetDateYear = $_POST["goalMetDate".$i."Year"];
				$goalMetDate = $goalMetDateYear."-".$goalMetDateMonth."-".$goalMetDat1Day;
			} else {
				$goalMetDate = null;
			}
			



   $ilp_goal_query = "select * from ilpcsr_goal where ilp_id = $ilpId and goal_nbr = $i";

   if($row = mysql_fetch_array(mysql_query($ilp_goal_query))) {

	$ilp_goal_insert = "update ilpcsr_goal 
                            set coordinator_involved = '".$coordinatorInvolved."', 
                                ilp_objective_id = ".$ilpObjective.", 
                                goal_set_date = '".$goalSetDate."', 
                                goal_target_date = '".$goalTargetDate."', 
                                goal_met_date = '".$goalMetDate."' 
                            where ilp_id = ".$ilpId." 
                            and goal_nbr = ".$i;
			
   } else {
				$ilp_goal_insert = "insert into ilpcsr_goal (id, ilp_id, goal_nbr, coordinator_involved, ilp_objective_id,
					goal_set_date, goal_target_date, goal_met_date) VALUES
					(NULL, ".$ilpId.", ".$i.", '".$coordinatorInvolved."', ".$ilpObjective.", '".$goalSetDate."', '".
					$goalTargetDate."', '".$goalMetDate."')";
			} 
			
			if(mysql_query($ilp_goal_insert)) {
				//echo "<br/>Success <br/>".$ilp_goal_insert;
			} else {			
				//echo "<br/>Failure <br/>".$ilp_goal_insert;
			}
		} 
	}

  header("Location:../admin.php?action=cieedit&id=".$id."&Status=ILP/CSR Form Submitted Successfully");
  exit();

   //FOR DEBUGGING:  comment out when done
   //echo "<br/>Success <br/>".$query;
} else {
	
   //header("Location:../admin.php?action=cieedit&id=".$id."&Error=Unable to process ILP/CSR Form due to system error.  Please contact system administrator.");
     
   //FOR DEBUGGING:  comment out when done
   echo "<br/>Failure <br/>".$query;
   echo "Could not connect: " . mysql_error();

 exit();
	//echo "<br/>Failure <br/>".$query;
}


?>